#------------------------------------------------------------------------------
#Class: PointCode
#Notes: There are 2 ways to organize objects set/list. 
#(1) The class constructor is to deal with a single object only. Defines class variables(hash, list)
# to store such objects. Define class methods to operate these objects, such as insert
# delete and update. This pm file demo this method. I think it is better to use method
# with the help of list/hash from perl.
#(2) Seperate the object, and objects set into two different classes. Such as define
# "class PointCodes" and "class PointCodeList" seperately. This may be good for c++/c language.
 
package PointCode;
use strict;

#class data/attributes. Can ONLY be accessed by this file's method since using "my".
#similar with static member data of C++.
my %pcList = ();

#static methods get the name of the calling class in there 1st parameter. Constructors
#are simply static methods.
#Constructor I - Default Constructor.
sub new
{
	my ($class) = shift;
	bless {
		name  => "",
		value => 0,
		cat   => "ITU"
	}, $class;
}

#Constructor II
#static method: 1st parameter = className.
sub CreatePC
{
	my ($class, $name, $value, $cat) = @_;
	#A reference is a scalar holding address of some var.
	#The following $self is a reference to the anonymous hash.
	my $self = 
		{
			name => $name,
			value => $value,
			cat => $cat
		};
	#Once an object has been blessed, you don't have to export symbols
	#with the @EXPORT_OK or @EXPORT arrays. In fact, as a general rule, 
	#if the module is trying to be object-oriented, then export nothing.
	bless($self, $class);
	
	return $self;
}

sub show()
{
	my $self = $_[0];
	print(  "\n\tPC: name = $self->{name}, value = $self->{value},"
		  . "cat = $self->{cat}" );
}

sub showAll()
{
	foreach my $pc ( values %pcList )
	{
		$pc->show();
	}
}

sub AddNewPC()
{
	my ($self, $name, $value, $cat)  = @_;

	return "" if ( exists $pcList{$name} );

	my $pc = new PointCode();
	$pc->{name}    = $name;
	$pc->{value}   = $value;
	$pc->{cat}     = $cat;
	$pcList{$name} = $pc;

	return ($name);
}

sub ReadPC()
{
	my $fileName = "./PointCode.txt";

	open( FP, "<$fileName" );

	while ( my $line = <FP> )
	{
		next if $line eq "" || $line =~ /^#.*/;

		my @fields = split( ' ', $line );

		my $pc = new PointCode();
		$pc->{name}            = $fields[0];
		$pc->{value}           = $fields[1];
		$pc->{cat}             = $fields[2];
		$pcList{ $pc->{name} } = $pc;
	}
	close FP;
}

1;
__END__
